package com.lb.shopguide.ui.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.lb.baselib.base.DialogBase;
import com.lb.shopguide.R;
import com.lb.shopguide.config.AppConfigManager;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;

import butterknife.OnClick;

/**
 * Create by kangzhao on 2018/12/4
 */
public class DialogGotoRecModule extends DialogBase implements View.OnClickListener {

    public static final int DIALOG_FIRST = 1;
    public static final int DIALOG_SECOND = 2;

    private int dialogType;
    private TextView tvGotoRec;
    private ImageView ivClose;
    private ImageView ivContent;

    public static DialogGotoRecModule getDialogGotoRecModule(int type) {
        DialogGotoRecModule dialogGotoRecModule = new DialogGotoRecModule();
        Bundle bundle = new Bundle();
        bundle.putInt(AppConstant.DIALOG_TYPE, type);
        dialogGotoRecModule.setArguments(bundle);
        return dialogGotoRecModule;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View mFragmentView = inflater.inflate(R.layout.dialog_goto_rec_module, container, false);
        initData();
        initView(mFragmentView);

//        setDialogPosition();
        bindData();
        return mFragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout(dm.widthPixels, dm.heightPixels);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.DIALOG_TYPE)) {
            dialogType = bundle.getInt(AppConstant.DIALOG_TYPE);
        }

    }


    private void initView(View view) {
        tvGotoRec = view.findViewById(R.id.tv_go_rec);
        ivClose = view.findViewById(R.id.iv_close);
        ivContent = view.findViewById(R.id.iv_dialog_content);

        switch (dialogType) {
            case DIALOG_FIRST:
                ivContent.setImageResource(R.drawable.iv_rec_guide_dialog_1);
                tvGotoRec.setText("我要赚钱");
                break;
            case DIALOG_SECOND:
                ivContent.setImageResource(R.drawable.iv_rec_guide_dialog_2);
                tvGotoRec.setText("如何赚钱");
                break;
        }


        ivClose.setOnClickListener(this);
        tvGotoRec.setOnClickListener(this);
    }

    private void bindData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                DialogGotoRecModule.this.dismissAllowingStateLoss();
                break;
            case R.id.tv_go_rec:
                if (mOnDialogClickListener != null) {
                    mOnDialogClickListener.gotoRec();
                    DialogGotoRecModule.this.dismissAllowingStateLoss();
                }
                break;
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mOnDialogClickListener != null) {
            mOnDialogClickListener.dialogDismiss();
        }
    }

    public interface OnDialogClickListener {
        void gotoRec();

        void dialogDismiss();
    }

    private OnDialogClickListener mOnDialogClickListener;

    public void setOnDialogClickListener(OnDialogClickListener listener) {
        mOnDialogClickListener = listener;
    }

}
